package com.fas.system.controller;

import com.fas.common.core.controller.BaseController;
import com.fas.common.core.domain.AjaxResult;
import com.fas.common.core.domain.entity.SysDept;
import com.fas.common.core.domain.model.LoginUser;
import com.fas.common.core.page.TableDataInfo;
import com.fas.common.utils.SecurityUtils;
import com.fas.system.domain.FasWordOrder;
import com.fas.system.service.IFasWordOrderService;
import com.fas.system.service.ISysDeptService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * 工单中心 Controller
 *
 * @date 2022-07-12
 */
@Api(tags = "工单中心")
@RestController
@RequestMapping("/system/wordOrder")
public class FasWordOrderController extends BaseController {

    @Resource
    private IFasWordOrderService fasWordOrderService;
    @Resource
    private ISysDeptService deptService;


    /**
     * 小程序巡检员-个人中心-我的工单
     */
    @ApiOperation(value = "小程序巡检员-个人中心-我的工单")
    @GetMapping("/miniappIpqcCenterOrderList")
    public TableDataInfo miniappIpqcCenterOrderList(Integer wordOrderStatus,Integer orderType,Integer pageNum,Integer pageSize){
        startPage();
        Integer userId = SecurityUtils.getUserId().intValue();
        return getDataTable(fasWordOrderService.miniappIpqcCenterOrderList(wordOrderStatus,orderType,userId));
    }


    /**
     * 小程序维保单位-工单管理
     */
    @ApiOperation(value = "小程序维保单位-工单管理-进行中的工单")
    @GetMapping("/miniappManOrder")
    public TableDataInfo miniappManorder(Integer cooperationCompanyId){
        return getDataTable(fasWordOrderService.miniappManorder(getDeptId(),cooperationCompanyId));
    }

    /**
     * 小程序维保单位-工单管理-工单详情
     */
    @ApiOperation(value = "小程序维保单位-工单管理-工单详情")
    @GetMapping("/miniappManOrderDetail")
    public AjaxResult miniappManorderDetail(Integer wordOrderId,Integer orderType){
        return AjaxResult.success(fasWordOrderService.miniappManorderDetail(wordOrderId,orderType));
    }

    /**
     * 小程序维保单位-工单管理-工单详情-同意或拒绝
     */
    @ApiOperation(value = "小程序维保单位-工单管理-工单详情-同意或拒绝")
    @PostMapping("/miniappManOrderDetailResult")
    public AjaxResult miniappManOrderDetailResult(@RequestBody FasWordOrder fasWordOrder){
        Long deptId = SecurityUtils.getDeptId();
        fasWordOrder.setManDeptId(deptId.intValue());
        return toAjax(fasWordOrderService.miniappManorderDetailResult(fasWordOrder));
    }

    /**
     * 小程序维保单位-工单中心-工单列表-工程师列表
     */
    @ApiOperation(value = "小程序维保单位-工单中心-工单列表-工程师列表")
    @GetMapping("/miniappEngineerList")
    public TableDataInfo miniappEngineerList(Integer pageNum,Integer pageSize,String permanentResidence){
        startPage();
        return getDataTable(fasWordOrderService.miniappEngineerList(permanentResidence));
    }

    /**
     * 小程序维保单位-个人中心-工单中心-工单列表
     */
    @ApiOperation(value = "小程序维保单位-个人中心-工单中心-工单列表")
    @GetMapping("/miniappManCenterOrderList")
    public TableDataInfo miniappManCenterOrderList( Integer wordOrderStatus,Long deptId,Integer cooperationCompanyId,Integer pageNum,Integer pageSize){
        if (deptId == null){
            deptId = getDeptId();
        }
        startPage();
        return getDataTable(fasWordOrderService.miniappManCenterOrderList(wordOrderStatus,deptId,cooperationCompanyId));
    }

    /**
     * 小程序工程师-工单-工单列表
     */
    @ApiOperation(value = "小程序工程师-工单-工单列表")
    @GetMapping("/miniappEngineerOrder")
    public TableDataInfo miniappEngineerOrder(Integer pageNum,Integer pageSize,String startTime,String endTime,Integer cooperationCompanyId,Integer engineerResult){
        LoginUser user = SecurityUtils.getLoginUser();
        SysDept sysDept = deptService.selectDeptById(user.getDeptId());
        Integer userId = null;
//        DeptType :  0:总部 1:渠道商 2:维保单位
//        if (!"0".equals(sysDept.getDeptType())) {
            userId = user.getUserId().intValue();
//        }
        startPage();
        return getDataTable(fasWordOrderService.miniappEngineerOrder(userId,startTime,endTime,cooperationCompanyId,engineerResult));
    }

    /**
     * 小程序工程师-工单-工单列表-工单详情
     */
    @ApiOperation(value = "小程序工程师-工单-工单列表-工单详情")
    @GetMapping("/miniappEngineerOrderDetail")
    public AjaxResult miniappEngineerOrderDetail(Integer wordOrderId,Integer orderType){
        return AjaxResult.success(fasWordOrderService.miniappEngineerOrderDetail(wordOrderId,orderType));
    }


    /**
     * 小程序工程师-工单-工单列表-工单详情-同意或拒绝
     */
    @ApiOperation(value = "小程序工程师-工单-工单列表-工单详情-同意或拒绝")
    @PostMapping("/miniappEngineerOrderDetailResult")
    public AjaxResult miniappEngineerOrderDetailResult(@RequestBody FasWordOrder fasWordOrder){
        fasWordOrder.setEngineerUserId(SecurityUtils.getUserId().intValue());
        Long userId = SecurityUtils.getUserId();
        return toAjax(fasWordOrderService.miniappEngineerOrderDetailResult(fasWordOrder,userId.intValue()));
    }


    /**
     * 小程序工程师-个人中心-工单中心-工单列表
     */
    @ApiOperation(value = "小程序工程师-个人中心-工单中心-工单列表")
    @GetMapping("/miniappEngineerCenterOrderList")
    public TableDataInfo miniappEngineerCenterOrderList(Integer wordOrderStatus,Integer engineerResult,Integer cooperationCompanyId,Long deptId){

        Integer userId = null;
        deptId = getDeptId();
        String userType = getLoginUser().getUser().getUserType();
        Integer status = 2;// 总公司查询工程师的工单
        if (userType.equals("01")){//小程序工程师端
            userId = getUserId().intValue();
            status = null;
            deptId = null;
        }
        return getDataTable(fasWordOrderService.miniappEngineerCenterOrderList(wordOrderStatus,userId,engineerResult,cooperationCompanyId,status,deptId));
    }

}
